package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 死信队列 配置
 * 死信的时间设置有二种方式
 * 第一种：在发送消息的时候设置时间
 * 第二种：设置全局时间
 */
@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";


    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", exchange_dead); //
        arguments.put("x-dead-letter-routing-key", routing_dead_2);
        //方式二，统一延迟时间
        arguments.put("x-message-ttl", 3000);
        //方式一与方式二切换时，必须先删除对应交换机与队列，否则出错
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable(queue_dead_2).build();
    }
    @Bean
    public Binding exchangeToQuery1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1).noargs();
    }
    @Bean
    public Binding exchangeToQuery2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2).noargs();
    }
}
